#include "Thread.hpp"
#include <stdio.h>
#include <string.h>
#include <pthread.h>

namespace wd{
Thread::Thread()
:_isRunning(false)
,_pthid(0)
{
}
void Thread::start(){
    if(!_isRunning){
        int ret = pthread_create(&_pthid,nullptr,start_routine,this);
        if(ret != 0){
            fprintf(stderr,"%s",strerror(ret));
            return ;
        }
        _isRunning = true;
    }
}
//子线程入口函数
void* Thread::start_routine(void* arg){
    Thread * pthread = static_cast<Thread*>(arg);
    if(pthread){
        pthread->run();
    }
    return nullptr;
}

void Thread::join(){
    if(_isRunning){
        pthread_join(_pthid,nullptr);

        _isRunning = false;
    }
}


}
